package com.mdc.mansion.core.modules.basedata.impl;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.core.modules.basedata.AppVersionService;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.basedata.dao.AppVersionDao;
import com.mdc.mansion.dataaccess.modules.basedata.entity.AppVersion;

/**
 * app版本信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class AppVersionServiceImpl extends CrudService<AppVersionDao, AppVersion> implements AppVersionService {

    @Override
    public AppVersion getVersionInfo(String data) {
        String channel="ALL";
        String platform="android";
        String version = "1.0";
        if(StringUtils.isNotBlank(data)){
            JSONObject jsonObject=JSONObject.parseObject(data);
            if(jsonObject.containsKey("channel")){channel=jsonObject.getString("channel");}
            if(jsonObject.containsKey("platform")){platform=jsonObject.getString("platform");}
            if(jsonObject.containsKey("version")){version=jsonObject.getString("version");}
        }
        
        AppVersion appVersion=new AppVersion();
        appVersion.setChannel(channel);
        appVersion.setPlatform(platform);
        appVersion.setDelFlag("0");
        Page<AppVersion> page = new Page<AppVersion>(-1);
        page.setOrderBy(" cast(version as decimal(9,2)) desc ");
        appVersion.setPage(page);
        List<AppVersion> list = this.findList(appVersion);
        /***新增判断,如果最新版本大于客户端输入版本,则反馈新版本信息**/
        if(CollectionUtils.isNotEmpty(list))
        {
        	AppVersion versionApp = list.get(0);
        	////对当前时间和生效时间进行比对,当前时间大于生效时间才返回数据
//        	Calendar.getInstance().getTime().getTime();
//        	versionApp.getOnlineTime().getTime();
        	
        	if(this.compareAPPVersion(version,versionApp.getVersion()))
        	{
        		if(Calendar.getInstance().getTime().getTime() >= versionApp.getOnlineTime().getTime())
        		{
        			return list.get(0);
        		}
        	}
        }
        
        return null;
    }

    /**
     * 对版本号进行判断
     * 版本号进行分段处理,以最新的版本号长度进行循环处理
     * @param inputVersion
     * @param newVersion
     * @return
     */
	private boolean compareAPPVersion(String inputVersion, String newVersion) {
		String [] inputVersionArray = inputVersion.split("\\.");
		String [] newVersionArray = newVersion.split("\\.");
		int i = newVersionArray.length;
		
		for(int n = 0 ; n < i ; n++)
		{
			BigDecimal inputLength = new BigDecimal(inputVersionArray[n]);
			BigDecimal newLength = new BigDecimal(newVersionArray[n]);
			if(newLength.compareTo(inputLength) > 0)  
			{
				return true;
			}
			
		}
		
		return false;
	}
}
